<?php 
	include("/includes/header.php");
	include("/includes/sidebar.php"); 

	$act = $_GET['act'];
	$id = $_GET['id'];

	if ($id != "") {
		$sql_user = "SELECT * FROM user WHERE id = $id";
		$query_user = mysql_query($sql_user);
		$result_user = mysql_fetch_array($query_user);
	}
?>

<div class="header">
	<span>
		<a href="user.php">
			<i class="fa fa-user"></i> User List
		</a>
	</span>
	<span>
		<a href="user-form.php?act=new">
			<i class="fa fa-plus"></i> Add New User
		</a>
	</span>
</div>

<form class="form-horizontal" data-id="<?php echo $id ?>">
	<div class="content row-fluid"> 
		<div class="span8">

			<input type="hidden" class="act" value="<?php echo $act; ?>">
			<div class="alert_container"></div>

    	<div>
    		<div>
    			<div class="alert alert-info">
    				<h4>
	    				<?php 
	    					if ($act == "edit") {
	    						echo "Edit User Form";
	    					} else {
	    						echo "New User Form";
	    					} 
	    				?>
    				</h4>
			    </div>
					<div class="control-group">
						<label class="control-label">Name</label>
						<div class="controls">
							<input placeholder="Input name" type="text" name="nama" class="nama span6 required" data-toggle="tooltip" title="Input name" required value="<?php echo $result_user[nama] ?>">
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">User type</label>
						<div class="controls">
							<select name="idtipe_user" data-toggle="tooltip" class="idtipe_user span6 required" title="Choose user type" required>
								<option name="idtipe_user" value="">-----  Choose user type  -----</option>
								<?php  
									$sql_tipe_user = "SELECT * FROM tipe_user";
									$query_tipe_user = mysql_query($sql_tipe_user);
									while ($result_tipe_user = mysql_fetch_array($query_tipe_user)) {
										if ($result_tipe_user[id] == $result_user[idtipe_user]) {
											$selected = "selected='selected'";
										} else {
											$selected = "";
										}
								?>
										<option name="idtipe_user" value="<?php echo $result_tipe_user[id] ?>" <?php echo $selected; ?>>
											<?php echo $result_tipe_user[nama]; ?>
										</option>
								<?php
									}
								?>
							</select>
						</div>
					</div>
					<div class="control-group input-append">
						<label class="control-label">Username</label>
						<div class="controls">
							<input id="appendedInput" placeholder="Input Username" type="text" name="username" class="username span6 required" data-toggle="tooltip" title="Input username" required value="<?php echo $result_user[username] ?>">
							<span class="add-on user_check" title="Available check" data-toggle="tooltip">
								<i class="fa fa-eye"></i>
							</span>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">Password</label>
						<div class="controls">
							<input placeholder="Input password" type="password" name="password" class="password span6 required" data-toggle="tooltip" title="Input password" required>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">Password Confirmation</label>
						<div class="controls">
							<input placeholder="Input password confirmation" type="password" name="konfirmasi_password" class="konfirmasi_password span6 required" data-toggle="tooltip" title="Input password confirmation" required>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">Email</label>
						<div class="controls">
							<input placeholder="Input email" type="email" name="email" class="email span6" data-toggle="tooltip" title="Input email" value="<?php echo $result_user[email] ?>">
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">Telepon</label>
						<div class="controls">
							<input placeholder="Input phone number" type="text" name="telepon" class="telepon span6" data-toggle="tooltip" title="Input phone number" value="<?php echo $result_user[telepon] ?>">
						</div>
					</div>
    		</div>
    	</div>
			<div>
				<div class="span12">
					<ul class="breadcrumb well">
						<button type="submit" class="btn btn-info btn-simpan">
							<i class="fa fa-check"></i>
							Save
						</button>
					</ul>
				</div>
			</div>
		</div>

		<div class="span4">
			<div class="alert alert-info">
				<h4>Profile Picture</h4>
	    </div>
	    <div class="foto_profil">
    		<img src="/img/default.jpg">
	    </div>
	    <div>
				<div class="span12">
					<ul class="breadcrumb well">
						<span class="btn btn-info btn-pilih">
							<i class="fa fa-white fa-upload"></i>
							Choose image
						</span>
					</ul>
				</div>
	    </div>
		</div>
	</div>
</form>

<div class="footer">
</div>

<?php include("/includes/footer.php") ?>       

<SCRIPT TYPE="text/javascript">
	$(document).ready(function() {
		$('.btn-simpan').click(function(event) {
			event.preventDefault();
			var is_valid = true;

			if (validate_form()) {

				if ($('.password').val() != $('.konfirmasi_password').val()) {
					alert_text("Password yang anda masukkan tidak cocok", "danger");
					$('.alert_container .alert_val:last').slideDown(function() {
						$('.konfirmasi_password').focus();
					});
					is_valid = false;
				} 

				var is_valid = true;
				var formUser = {
					id: $('form').data('id'),
					username: $('.username').val(),
					act: 4
				}

				$.ajax({
					url: '/action/act_user.php',
					type: 'POST',
					data: formUser,
					cache: false, 
			    success: function(data){
						if (data == 0) {
							alert_text("Username is available", "success");
							$('.alert_container .alert_val:last').slideDown();
							is_valid = false;
						} else {
							alert_text("Username is already used", "danger");
							$('.alert_container .alert_val:last').slideDown();
						}
			    }
				})
				
				if ($('.act').val() == "edit") {
					act_form = "edit";
					act = 2;
					alert_msg = "Data has successfully edited";
				} else {
					act_form = "new";
					act = 1;
					alert_msg = "Data has successfully added";
				}

				if (is_valid) {
					var formUser = {
						id: $('form').data('id'),
						nama: $('.nama').val(),
						username: $('.username').val(),
						idtipe_user: $('.idtipe_user').val(),
						password: $('.password').val(),
						email: $('.email').val(),
						telepon: $('.telepon').val(),
						act: act,
						act_form: act_form
					}

					$.ajax({
						url: '/action/act_user.php',
						type: 'POST',
						data: formUser,
						cache: false, 
		        success: function(data){
		        	if (data == "sukses") {
			        	alert(alert_msg);
			        	// window.location = "user.php";
		        		location.reload();
		        	} else {
		        		alert(data);
		        	}
		        }
					})
				}
			};
		});

		$('.user_check').click(function(event) {
			var is_valid = true;
			var formUser = {
				id: $('form').data('id'),
				username: $('.username').val(),
				act: 4
			}

			$.ajax({
				url: '/action/act_user.php',
				type: 'POST',
				data: formUser,
				cache: false, 
		    success: function(data){
					if (data == 0) {
						alert_text("Username dapat digunakan", "success");
						$('.alert_container .alert_val:last').slideDown();
					} else {
						alert_text("Username telah digunakan", "danger");
						$('.alert_container .alert_val:last').slideDown();
					}
		    }
			})
		});
	});
</SCRIPT>